﻿using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace GFrame
{
    public class FileService : Singleton<FileService>
    {
        public List<string> GetFiles(string path, string searchPattern, bool recursive)
        {
            List<string> fileNames = new List<string>();
            if ( null == path || null == searchPattern)
            {
                return fileNames;
            }

            if (path.Last() != '/')
            {
                path += '/';
            }

            try
            {
                string[] allFiles = Directory.GetFiles(path, searchPattern,
                    recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

                for (int index = 0; index < allFiles.Length; ++index)
                {
                    string filePath = allFiles[index];
                    if (filePath.Length > path.Length)
                    {
                        fileNames.Add(PathUtil.NormalizePath(filePath.Substring(path.Length)));
                    }
                }
            }
            catch (Exception)
            {
                // ignored
            }
            return fileNames;
        }
    }

}
